package com.zhr.tea.view.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.bumptech.glide.Glide;
import com.sunfusheng.marqueeview.MarqueeView;
import com.zhr.tea.R;
import com.zhr.tea.entity.shouye.Banner;
import com.zhr.tea.entity.shouye.BodyCentent;
import com.zhr.tea.entity.shouye.End;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ym on 2016/11/1.
 */

public class ShouyeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    public static final int TYPE1 = 0;
    public static final int TYPE2 = 1;
    public static final int TYPE3 = 2;
    public static final int TYPE4 = 3;
    public static final int TYPE5 = 4;
    public static final int TYPE6 = 5;
    public static final int TYPE7 = 6;


    private List<Banner.AdColumnListBean> banner;
    private List<End.ContentBean> end;
    private List<BodyCentent.ContentBean.ThemeListBean> themeListBeen;
    private List<BodyCentent.ContentBean.ChannelListBean> channelListBeen;
    private List<BodyCentent.ContentBean.BenefitSaleBean.GoodsListBean> benefitSaleBeen;
    private Context context;
    private BannerListen bannerListen = null;
    private RecycleClickListener recycleClickListener;

    public ShouyeAdapter(List<Banner.AdColumnListBean> banner, List<BodyCentent.ContentBean.BenefitSaleBean.GoodsListBean> benefitSaleBeen, List<BodyCentent.ContentBean.ChannelListBean> channelListBeen, Context context, List<End.ContentBean> end, List<BodyCentent.ContentBean.ThemeListBean> themeListBeen, BannerListen bannerListen) {
        this.banner = banner;
        this.benefitSaleBeen = benefitSaleBeen;
        this.channelListBeen = channelListBeen;
        this.context = context;
        this.end = end;
        this.themeListBeen = themeListBeen;
        this.bannerListen = bannerListen;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        RecyclerView.ViewHolder holder;
        switch (viewType) {
            case TYPE1:
                view = LayoutInflater.from(context).inflate(R.layout.item_shouye_type1, null);
                holder = new ViewHolder1(view);
                return holder;
            case TYPE2:
                view = LayoutInflater.from(context).inflate(R.layout.item_shouye_type2, null);
                holder = new ViewHolder2(view);
                return holder;
            case TYPE3:
                view = LayoutInflater.from(context).inflate(R.layout.item_shouye_type3, null);
                holder = new ViewHolder3(view);
                return holder;
            case TYPE4:
                view = LayoutInflater.from(context).inflate(R.layout.item_shouye_type4, null);
                holder = new ViewHolder4(view);
                return holder;
            case TYPE5:
                view = LayoutInflater.from(context).inflate(R.layout.item_shouye_type5, null);
                holder = new ViewHolder5(view);
                return holder;
            case TYPE6:
                view = LayoutInflater.from(context).inflate(R.layout.item_shouye_type6, null);
                holder = new ViewHolder6(view);
                return holder;
            case TYPE7:
                view = LayoutInflater.from(context).inflate(R.layout.item_shouye_type7, null);
                holder = new ViewHolder7(view);
                return holder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ViewHolder1) {
            ((ViewHolder1) holder).banner.setPages(new CBViewHolderCreator<LocalImageHolderView>() {
                @Override
                public LocalImageHolderView createHolder() {
                    return new LocalImageHolderView();
                }
            }, banner).stopTurning();
            ((ViewHolder1) holder).banner.startTurning(3000);
            ((ViewHolder1) holder).banner.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    bannerListen.bannerListen(position);
                }
            });
            ((ViewHolder1) holder).tvQiandao.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bannerListen.textType1Listen();
                }
            });
            ((ViewHolder1) holder).tvYouhui.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bannerListen.textType2Listen();
                }
            });
            ((ViewHolder1) holder).tvChayi.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bannerListen.textType3Listen();
                }
            });
            ((ViewHolder1) holder).tvZhongchou.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bannerListen.textType4Listen();
                }
            });
            ((ViewHolder1) holder).tvJiaoliu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bannerListen.textType5Listen();
                }
            });
        } else if (holder instanceof ViewHolder2) {
            List<String> info = new ArrayList<>();
            for (int i = 0; i < channelListBeen.size(); i++) {
                info.add(channelListBeen.get(i).getWord());
            }
            ((ViewHolder2) holder).marqueeview.startWithList(info);

        } else if (holder instanceof ViewHolder3) {
            if (benefitSaleBeen.size() != 0) {
                ((ViewHolder3) holder).tvShouye.setText("限时特惠");
                ShouyeXianshiAdapter adapter = new ShouyeXianshiAdapter(context, benefitSaleBeen);
                ((ViewHolder3) holder).rlv.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
                ((ViewHolder3) holder).rlv.setAdapter(adapter);
            } else {
                ((ViewHolder3) holder).tvShouye.setVisibility(View.GONE);
                ((ViewHolder3) holder).rlv.setVisibility(View.GONE);
            }
        } else if (holder instanceof ViewHolder4) {
            Glide.with(context).load(themeListBeen.get(position - 3).getThemeTitle()).placeholder(R.mipmap.abc_popup_background_mtrl_mult).into(((ViewHolder4) holder).ivThemetitle);
            Glide.with(context).load(themeListBeen.get(position - 3).getSubThemeList().get(0).getSubThemePhoto()).placeholder(R.mipmap.diy_bg).into(((ViewHolder4) holder).ivSubphoto);
            ((ViewHolder4) holder).ivSubphoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bannerListen.tvType4Listen(position - 3);
                }
            });
            Glide.with(context).load(themeListBeen.get(position - 3).getSubThemeList().get(1).getSubThemePhoto()).placeholder(R.mipmap.diy_bg).into(((ViewHolder4) holder).ivSubphoto1);
            ((ViewHolder4) holder).ivSubphoto1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bannerListen.tvType4Listen1(position - 3);
                }
            });
            Glide.with(context).load(themeListBeen.get(position - 3).getSubThemeList().get(2).getSubThemePhoto()).placeholder(R.mipmap.diy_bg).into(((ViewHolder4) holder).ivSubphoto2);
            ((ViewHolder4) holder).ivSubphoto2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bannerListen.tvType4Listen2(position - 3);
                }
            });
        } else if (holder instanceof ViewHolder5) {
            Glide.with(context).load(themeListBeen.get(position - 3).getThemeTitle()).placeholder(R.mipmap.diy_bg).into(((ViewHolder5) holder).ivThemetitle);
            ((ViewHolder5) holder).ivSubphoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bannerListen.tvType5Listen(position-3);
                }
            });
            Glide.with(context).load(themeListBeen.get(position - 3).getSubThemeList().get(0).getSubThemePhoto()).placeholder(R.mipmap.diy_bg).into(((ViewHolder5) holder).ivSubphoto);
            ((ViewHolder5) holder).ivSubphoto1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bannerListen.tvType5Listen1(position-3);
                }
            });
            Glide.with(context).load(themeListBeen.get(position - 3).getSubThemeList().get(1).getSubThemePhoto()).placeholder(R.mipmap.diy_bg).into(((ViewHolder5) holder).ivSubphoto1);
                ((ViewHolder5) holder).ivSubphoto2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        bannerListen.tvType5Listen2(position-3);
                    }
                });
            ((ViewHolder5) holder).ivSubphoto3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bannerListen.tvType5Listen3(position-3);
                }
            });
            Glide.with(context).load(themeListBeen.get(position - 3).getSubThemeList().get(2).getSubThemePhoto()).placeholder(R.mipmap.diy_bg).into(((ViewHolder5) holder).ivSubphoto2);
            Glide.with(context).load(themeListBeen.get(position - 3).getSubThemeList().get(3).getSubThemePhoto()).placeholder(R.mipmap.diy_bg).into(((ViewHolder5) holder).ivSubphoto3);
        } else if (holder instanceof ViewHolder6) {
            ((ViewHolder6) holder).tvMore.setImageResource(R.mipmap.index_icon_into);
            ((ViewHolder6) holder).tvMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bannerListen.tvType6Listen();
                }
            });
        } else if (holder instanceof ViewHolder7) {
            holder.itemView.setTag(end.get(position - 4 - themeListBeen.size()).getCode());
            holder.itemView.setOnClickListener(this);
            ((ViewHolder7) holder).tvName.setText(end.get(position - 4 - themeListBeen.size()).getGoodsName());
            ((ViewHolder7) holder).tvDes.setText(end.get(position - 4 - themeListBeen.size()).getGoodsDes());
            Glide.with(context).load(end.get(position - 4 - themeListBeen.size()).getContent()).placeholder(R.mipmap.diy_bg).into(((ViewHolder7) holder).ivContent);
            ((ViewHolder7) holder).tvTag.setText(end.get(position - 4 - themeListBeen.size()).getTag());
            ((ViewHolder7) holder).tvTag.setTextColor(Color.parseColor(end.get(position - 4 - themeListBeen.size()).getTagColor().trim()));
            ((ViewHolder7) holder).tvPrice.setText(end.get(position - 4 - themeListBeen.size()).getPrice() + "元");
            ((ViewHolder7) holder).tvOldprice.setText("市场价" + end.get(position - 4 - themeListBeen.size()).getMarketPrice() + "元");
            ((ViewHolder7) holder).ivCar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bannerListen.ivCarListen(position - 4 - themeListBeen.size());
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE1;
        } else if (position == 1) {
            return TYPE2;
        } else if (position == 2) {
            return TYPE3;
        } else if (position > 2 && position < themeListBeen.size() + 3) {
            if (themeListBeen.get(position - 3).getThemeId() == 9) {
                return TYPE5;
            } else {
                return TYPE4;
            }
        } else if (position == themeListBeen.size() + 3) {
            return TYPE6;
        } else {
            return TYPE7;
        }

    }

    @Override
    public int getItemCount() {
        return 4 + themeListBeen.size() + end.size();
    }


    class LocalImageHolderView implements Holder<Banner.AdColumnListBean> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);

            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, Banner.AdColumnListBean data) {
            Glide.with(context).load(banner.get(position).getAdCoverPhoto()).into(imageView);
        }
    }

    class ViewHolder1 extends RecyclerView.ViewHolder {
        @BindView(R.id.banner)
        ConvenientBanner banner;
        @BindView(R.id.tv_qiandao)
        TextView tvQiandao;
        @BindView(R.id.tv_youhui)
        TextView tvYouhui;
        @BindView(R.id.tv_chayi)
        TextView tvChayi;
        @BindView(R.id.tv_zhongchou)
        TextView tvZhongchou;
        @BindView(R.id.tv_jiaoliu)
        TextView tvJiaoliu;

        ViewHolder1(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ViewHolder2 extends RecyclerView.ViewHolder {
        @BindView(R.id.marqueeView)
        MarqueeView marqueeview;

        ViewHolder2(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ViewHolder3 extends RecyclerView.ViewHolder {
        @BindView(R.id.rlv)
        RecyclerView rlv;
        @BindView(R.id.tv_shouye)
        TextView tvShouye;

        ViewHolder3(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ViewHolder4 extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_themetitle)
        ImageView ivThemetitle;
        @BindView(R.id.iv_subphoto)
        ImageView ivSubphoto;
        @BindView(R.id.iv_subphoto1)
        ImageView ivSubphoto1;
        @BindView(R.id.iv_subphoto2)
        ImageView ivSubphoto2;

        ViewHolder4(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ViewHolder5 extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_themetitle)
        ImageView ivThemetitle;
        @BindView(R.id.iv_subphoto)
        ImageView ivSubphoto;
        @BindView(R.id.iv_subphoto1)
        ImageView ivSubphoto1;
        @BindView(R.id.iv_subphoto2)
        ImageView ivSubphoto2;
        @BindView(R.id.iv_subphoto3)
        ImageView ivSubphoto3;

        ViewHolder5(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ViewHolder6 extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_more)
        ImageView tvMore;

        ViewHolder6(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ViewHolder7 extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.iv_content)
        ImageView ivContent;
        @BindView(R.id.tv_des)
        TextView tvDes;
        @BindView(R.id.tv_tag)
        TextView tvTag;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_oldprice)
        TextView tvOldprice;
        @BindView(R.id.iv_shouye_car)
        ImageView ivCar;

        ViewHolder7(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface RecycleClickListener {
        void itemClick(View view, int code);
    }

    @Override
    public void onClick(View view) {
        if (recycleClickListener != null) {
            recycleClickListener.itemClick(view, (Integer) view.getTag());
        }
    }

    public void setOnitemClickListener(RecycleClickListener listener) {
        this.recycleClickListener = listener;
    }

    public interface BannerListen {
        void bannerListen(int position);

        void textType1Listen();

        void textType2Listen();

        void textType3Listen();

        void textType4Listen();

        void textType5Listen();

        void tvType4Listen(int position);

        void tvType4Listen1(int position);

        void tvType4Listen2(int position);

        void tvType5Listen(int position);

        void tvType5Listen1(int position);

        void tvType5Listen2(int position);

        void tvType5Listen3(int position);
        void tvType6Listen();

        void ivCarListen(int position);
    }
}
